/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import coba.DataModel;
import coba.TestComboBox;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFReader;
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
//import javax.swing.ComboBoxModel;
import koneksi.Koneksi;
import projectkp.DataFile;

/**
 *
 * @author rosi
 */
public class Home extends javax.swing.JFrame {

    private DataFile data;
    private File file;
    private FileInputStream inStream;
    private int pilih;
    private DBFReader reader;
    private String fileName;
    private boolean shouldHide = false;
    private final Vector list = new Vector();
    private JComboBox combo;

    /** Creates new form Home */
    public Home() {
        initComponents();

        Collections.sort(DataModel.getData());

        combo = new JComboBox(comboBulan.getModel());

        JTextField field = (JTextField) comboBulan.getEditor().getEditorComponent();
        field.addKeyListener(new TestComboBox(combo));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        comboTahun = new javax.swing.JComboBox();
        comboBulan = new javax.swing.JComboBox(comboBulan.getModel());
        butttonCari = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        comboID = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        textNamaFile = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Showcard Gothic", 0, 18));
        jLabel1.setText("APLIKASI PENJUALAN BUKU");

        jLabel2.setText("ID PRODUCT");

        jLabel3.setText("TAHUN");

        jLabel4.setText("BULAN");

        comboTahun.setEditable(true);
        comboTahun.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }));
        comboTahun.setSelectedIndex(-1);
        comboTahun.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comboTahunMouseClicked(evt);
            }
        });
        comboTahun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboTahunActionPerformed(evt);
            }
        });

        comboBulan.setEditable(true);
        comboBulan.setSelectedIndex(-1);
        JTextField field = (JTextField) combo.getEditor().getEditorComponent();
        comboBulan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBulanActionPerformed(evt);
            }
        });

        butttonCari.setText("CARI");
        butttonCari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butttonCariActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Judul Buku", "Terjual", "null", "null"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        comboID.setEditable(true);
        comboID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboIDActionPerformed(evt);
            }
        });

        jLabel5.setText("NAMA FILE");

        jButton1.setText("BROWSE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 378, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(39, 39, 39)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textNamaFile, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(comboTahun, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(comboID, javax.swing.GroupLayout.Alignment.LEADING, 0, 169, Short.MAX_VALUE)
                                        .addComponent(comboBulan, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(205, 205, 205)
                        .addComponent(jLabel1)))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(butttonCari))
                .addGap(321, 321, 321))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 622, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(107, 107, 107))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(textNamaFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(comboID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(comboTahun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(7, 7, 7))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(butttonCari)
                                .addGap(17, 17, 17)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(comboBulan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboTahunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboTahunActionPerformed
        // TODO add your handling code here:

//        JComboBox combo;
//        
//        combo= new JComboBox(comboBulan.getModel());
//
//        JTextField field = (JTextField) combo.getEditor().getEditorComponent();
//        field.addKeyListener(new TestComboBox(combo));
    }//GEN-LAST:event_comboTahunActionPerformed

    public void getDataBase() {
//        0001JAN
//        String no = comboID.g
        String idBulan = "";
        String id = String.valueOf(comboID.getSelectedItem().toString());
        String bulan = String.valueOf(comboBulan.getSelectedItem().toString());
        String tahun = String.valueOf(comboTahun.getSelectedItem().toString());

        if (bulan.equalsIgnoreCase("Januari")) {
            idBulan = "01";
        } else if (bulan.equalsIgnoreCase("Februari")) {
            idBulan = "02";
        } else if (bulan.equalsIgnoreCase("Maret")) {
            idBulan = "03";
        } else if (bulan.equalsIgnoreCase("April")) {
            idBulan = "04";
        } else if (bulan.equalsIgnoreCase("Mei")) {
            idBulan = "05";
        } else if (bulan.equalsIgnoreCase("Juni")) {
            idBulan = "06";
        } else if (bulan.equalsIgnoreCase("Juli")) {
            idBulan = "07";
        } else if (bulan.equalsIgnoreCase("Agustus")) {
            idBulan = "08";
        } else if (bulan.equalsIgnoreCase("September")) {
            idBulan = "09";
        } else if (bulan.equalsIgnoreCase("Oktober")) {
            idBulan = "10";
        } else if (bulan.equalsIgnoreCase("November")) {
            idBulan = "11";
        } else if (bulan.equalsIgnoreCase("Desember")) {
            idBulan = "12";
        }

        String subBulan = bulan.substring(0, 3);
        String subTahun = tahun.substring(2);

        System.out.println(subTahun + "" + idBulan + "" + subBulan);
    }

//    @Override
    public void keyTypedBulan(final KeyEvent e) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                String text = ((JTextField) e.getSource()).getText();
                if (text.length() == 0) {
                    combo.getModel();
//                    setSuggestionModel(combo, new DefaultComboBoxModel(list), "");
                    combo.hidePopup();
                } else {
                    ComboBoxModel m = combo.getModel();
//                    ComboBoxModel m = getSuggestedModel(list, text);
                    if (m.getSize() == 0 || shouldHide) {
                        combo.hidePopup();
                    } else {
                        combo.setModel(m) ;
//                        setSuggestionModel(combo, m, text);
                        combo.showPopup();
                    }
                }
            }
        });
    }

    public void keyPressedBulan(KeyEvent e) {
        JTextField textField = (JTextField) e.getSource();
        String text = textField.getText();
        shouldHide = false;
        switch (e.getKeyCode()) {
            case KeyEvent.VK_RIGHT:
                for (Object obj : list) {
                    String s = (String) obj;
                    if (s.startsWith(text)) {
                        textField.setText(s);
                        return;
                    }
                }
                break;
            case KeyEvent.VK_ENTER:
                if (!list.contains(text)) {
                    list.addElement(text);
                    Collections.sort(list);
                    combo.getModel();
//                    setSuggestionModel(combo, getSuggestedModel(list, text), text);
                }
                shouldHide = true;
                break;
            case KeyEvent.VK_ESCAPE:
                shouldHide = true;
                break;
        }
    }

    private void comboTahunMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboTahunMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_comboTahunMouseClicked

    private void comboBulanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBulanActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboBulanActionPerformed

    private void butttonCariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butttonCariActionPerformed
        // TODO add your handling code here:
        getDataBase();
    }//GEN-LAST:event_butttonCariActionPerformed

    private void comboIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboIDActionPerformed
        // TODO add your handling code here:

        JComboBox combo = new JComboBox();
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
    }//GEN-LAST:event_comboIDActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        data = new DataFile();
        JFileChooser pilihFile = new JFileChooser();
        pilih = pilihFile.showOpenDialog(this);
        if (pilih == JFileChooser.APPROVE_OPTION) {
            try {
                file = pilihFile.getSelectedFile();

                inStream = new FileInputStream(file);
                reader = new DBFReader(inStream);

                data.setNamaFile(file.getName());

                fileName = data.getNamaFile();

                textNamaFile.setText(fileName);

                this.getComboID();
            } catch (DBFException ex) {
                Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public final void getComboID() {
        try {
            Connection conn = Koneksi.open();
            Statement st = conn.createStatement();

            String query = "select PROD_ID from " + fileName + " ";
            ResultSet res = st.executeQuery(query);

            Object[] rowObjects;
            while (res.next()) {
                rowObjects = reader.nextRecord();
                for (int i = 0; i < res.getMetaData().getColumnCount(); i++) {
                    comboID.addItem(rowObjects[i].toString() + "  ");
                }
            }
            st.close();
            inStream.close();
        } catch (DBFException ex) {
            System.out.println("error dbf exception : " + ex.getMessage());
        } catch (FileNotFoundException ex) {
            System.out.println("error file not found exception : " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("error sql exception : " + ex.getMessage());
        } catch (IOException ex) {
            System.out.println("error io exception : " + ex.getMessage());
        }
    }

//    private void setSuggestionModel(JComboBox comboBox, ComboBoxModel mdl, String str) {
//        comboBox=comboBulan;
//        comboBox.setModel(mdl);
//        comboBox.setSelectedIndex(-1);
//        ((JTextField) comboBox.getEditor().getEditorComponent()).setText(str);
//    }

//    private static ComboBoxModel getSuggestedModel(
//            Vector list, String text) {
//        DefaultComboBoxModel m = new DefaultComboBoxModel();
//        for (Object obj : list) {
//            String s = (String) obj;
//            if (s.toUpperCase().contains(text.toUpperCase())) {
//                m.addElement(s);
//            }
//        }
//        return m;
//    }

    public void browseFile() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Home().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butttonCari;
    private javax.swing.JComboBox comboBulan;
    private javax.swing.JComboBox comboID;
    private javax.swing.JComboBox comboTahun;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField textNamaFile;
    // End of variables declaration//GEN-END:variables
}
